import cfg from "../utils/config.js";
import { get, post, toast, createNonce32CharStr } from "../utils/util.js";

//获取token
const getUserTokenPath = cfg.websiteUrl + "/api/weixin/code?code=";
//获取过钥匙的景点列表
const getUserFinshPath = cfg.websiteUrl + "/api/user/opened-scenic";

const getUserTaskDoPath = cfg.websiteUrl + "/api/user/task-do";
//打开箱子
const getUserBoxOpenPath = cfg.websiteUrl + "/api/user/box-open";
//箱子奖励
const getUserBoxPrizeGetPath = cfg.websiteUrl + "/api/user/box-prize-get";
//箱子列表
const getUserBoxListPath = cfg.websiteUrl + "/api/user/box-list";
//用户信息
const getUserInfoPath = cfg.websiteUrl + "/api/user/userinfo";

const getHomeJsonPath =
	cfg.configUrl + "/home/home.json?" + createNonce32CharStr(12);

const getPersonNumPath = cfg.websiteUrl + "/api/user/scenic-passed";

const updateUserInfoPath = cfg.websiteUrl + "/api/user/updateuserinfo";

const getPuzzleInfoPath = cfg.configUrl;

/**
 * 定时刷新wx.login code
 * */
let timer = null;
const refreshLoginCode = () => {
	clearInterval(timer);
	uni.login({
		success: res => {
			getApp().globalData.code = res.code;
		}
	});
	timer = setTimeout(() => {
		refreshLoginCode();
	}, 3000000);
};

/**
 * 判断登录状态 和对应跳转
 * */
export const isLoginOk = config => {
	let wxUserInfo = getApp().globalData.phoneNum;
	if (wxUserInfo == null) {
		return false;
	}
	return true;
};

/**
 * code 换取 token
 * */
export const getUserToken = code => {
	return new Promise(async (resolve, reject) => {
		let res = await get({
			path: getUserTokenPath + code,
			params: {}
		});

		if (res.code !== 0) {
			toast("服务器开小差了,请重试");
			return;
		}
		console.log("-------登录--------");
		const userInfo = res.data;
		getApp().globalData.userInfo = userInfo;
		resolve(true);
	});
};

/**
 * 已开启的景点
 * */
export const getUserFinsh = config => {
	return new Promise(async (resolve, reject) => {
		let res = await get({
			path: getUserFinshPath,
			params: {}
		});

		if (res.code !== 0) {
			toast(res.message);
			return;
		}
		uni.setStorageSync("finshList", res.data);
		if (res && res.data && res.data.length > 0) {
			let addressList = uni.getStorageSync("addressList");
			if (addressList) {
				console.log(addressList)
				console.log(addressList)
				let addressListNew = addressList.map((item, index) => {
					if (res.data.indexOf(item.id) !== -1) {
						item.isShowKey = true;
					} else {
						item.isShowKey = false;
					}
					return item;
				});
				console.log((addressList), 'cdadsccdsaascd')
				uni.setStorageSync("addressList", addressList);
				console.log((addressList), '11111111')
			}
		}
		resolve(res.data);
	});
};

/**
 * 打开箱子
 * */
export const getUserTaskDo = config => {
	return new Promise(async (resolve, reject) => {
		let res = await post({
			path: getUserTaskDoPath,
			params: config.params
		});

		if (res.code !== 0) {
			toast(res.message);
			return;
		}
		resolve(res.data);
	});
};

/**
 * 打开箱子
 * */
export const getUserBoxOpen = config => {
	return new Promise(async (resolve, reject) => {
		let res = await post({
			path: getUserBoxOpenPath,
			params: config.params
		});

		if (res.code !== 0) {
			toast(res.message);
			return;
		}
		resolve(res.data);
	});
};

/**
 *
 * */
export const getUserBoxPrizeGet = config => {
	return new Promise(async (resolve, reject) => {
		let res = await post({
			path: getUserBoxPrizeGetPath,
			params: config.params
		});

		if (res.code !== 0) {
			toast(res.message);
			return;
		}
		resolve(res.data);
	});
};

/**
 *
 * */
export const getUserBoxList = config => {
	return new Promise(async (resolve, reject) => {
		let res = await get({
			path: getUserBoxListPath,
			params: {}
		});

		if (res.code !== 0) {
			toast(res.message);
			return;
		}
		resolve(res.data);
	});
};
/**
 * 获取信息
 * */
export const getUserInfo = config => {
	return new Promise(async (resolve, reject) => {
		let res = await get({
			path: getUserInfoPath,
			params: {}
		});

		if (res.code !== 0) {
			toast(res.message);
			return;
		}
		if (res.data.nickname || res.data.headimgurl) {
			let aa = uni.getStorageSync("wxUserInfo");
			if (!aa) aa = {};
			aa.nickName = res.data.nickname;
			aa.avatarUrl = res.data.headimgurl;
			getApp().globalData.wxUserInfo = aa;
			uni.setStorageSync("wxUserInfo", aa);
		} else if (res.data.phone) {
			getApp().globalData.phoneNum = res.data.phone;
			uni.setStorageSync("phoneNum", res.data.phone);
		}
		console.log(res)
		console.log('获取的用户信息')
		if (
			res.data.receiver_city != "" &&
			res.data.receiver_address != "" &&
			res.data.receiver_name != "" &&
			res.data.receiver_phone !=''
		) {
			let addressData = {
				receiver_city: res.data.receiver_city,
				receiver_address: res.data.receiver_address,
				receiver_name: res.data.receiver_name,
				receiver_phone: res.data.receiver_phone
			};
			uni.setStorageSync("addressData", addressData);
		}

		// getApp().globalData.phoneNum = "15517589670";
		// uni.setStorageSync("phoneNum", "15517589670");
		resolve(res.data);
	});
};

/**
 * 获取首页景点json文件
 * */
export const getHomeJson = config => {
	return new Promise(async (resolve, reject) => {
		let res = await get({
			path: getHomeJsonPath,
			params: {}
		});

		if (res.code !== 0) {
			toast(res.message);
			return;
		}
		uni.setStorageSync("addressList", res.data);
		resolve(res.data);
	});
};

/**
 * 获取信息
 * */
export const getPersonNum = config => {
	return new Promise(async (resolve, reject) => {
		let res = await get({
			path: getPersonNumPath,
			params: config.params
		});

		if (res.code !== 0) {
			toast(res.message);
			return;
		}
		resolve(res.data);
	});
};

/**
 * 获取信息
 * */
export const getPuzzleInfo = config => {
	return new Promise(async (resolve, reject) => {
		let res = await get({
			path: `${getPuzzleInfoPath}/puzzle/${
				config.id
			}.json?${createNonce32CharStr(12)}`,
			params: {}
		});

		if (res.code !== 0) {
			toast(res.message);
			return;
		}
		resolve(res.data);
	});
};

export const getDescInfo = config => {
	return new Promise(async (resolve, reject) => {
		let res = await get({
			path: `${getPuzzleInfoPath}/desc/${
				config.id
			}.json?${createNonce32CharStr(12)}`,
			params: {}
		});
		if (res.code !== 0) {
			toast(res.message);
			return;
		}

		resolve(res.data);
	});
};

/**
 * 更新用户头像昵称等信息
 * nickname  昵称 headimgurl  头像  phone 手机号  iv  iv   encrypted_data 加密密文
 * */
export const updateUserinfo = config => {
	return new Promise(async (resolve, reject) => {
		let res = await post({
			path: updateUserInfoPath,
			params: config.params
		});

		if (res.code !== 0) {
			toast(res.message);
			return;
		}
		resolve(res.data);
	});
};
